package com.ruoyi.common.mp.config;/**
 * @InterfaceName: EntityFillConfig
 * @Description:
 * @Author: YourName
 * @Date: 2023-04-12
 */


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.core.exception.auth.NotLoginException;
import com.ruoyi.common.core.exception.auth.NotPermissionException;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @InterfaceName: EntityFillConfig
 * @Description:
 * @Author: lilirui
 * @Date: 2023-04-12
 */
@Component

public class EntityFillConfig implements MetaObjectHandler {

    /**
     * 添加操作
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
//        设置属性值
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
        if (SecurityUtils.getLoginUser() == null) {
            throw new NotLoginException("当前用户未登录，请先登录");
        }
        this.setFieldValByName("createBy", SecurityUtils.getLoginUser().getUsername(), metaObject);
        this.setFieldValByName("updateBy", SecurityUtils.getLoginUser().getUsername(), metaObject);
    }

    /**
     * 修改操作
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
//        设置属性值
        this.setFieldValByName("updateTime", new Date(), metaObject);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            throw new NotLoginException("当前用户未登录，请先登录");

        }
        this.setFieldValByName("updateBy", loginUser.getUsername(), metaObject);
    }
}